#ifndef QUEUETOSTACK_H
#define QUEUETOSTACK_H

#include "Stack.h"
#include "LinkQueue.h"
#include "Exception.h"

namespace DSLib
{

template <typename T>
class QueueToStack : public Stack<T>
{
protected:
	LinkQueue<T> m_queue_1;
	LinkQueue<T> m_queue_2;
	LinkQueue<T>* m_pIn;
	LinkQueue<T>* m_pOut;

	void move() const
	{
		int n = m_pIn->length() - 1;

		for (int i = 0; i < n; i++)
		{
			m_pOut->add(m_pIn->front());
			m_pIn->remove();
		}
	}

	void swap()
	{
		LinkQueue<T>* temp = NULL;

		temp = m_pIn;
		m_pIn = m_pOut;
		m_pOut = temp;
	}

public:
	QueueToStack()
	{
		m_pIn = &m_queue_1;
		m_pOut = &m_queue_2;
	}

	void push(const T& e)
	{
		m_pIn->add(e);
	}

	void pop()
	{
		move();

		if (m_pIn->length() > 0)
		{
			m_pIn->remove();
			swap();
		}
		else
		{
			THROW_EXCEPTION(InvaildParameterException, "no element to be removed!");
		}
	}

	T top() const
	{
		move();

		if (m_pIn->length() > 0)
		{
			return m_pIn->front();
		}
		else
		{
			THROW_EXCEPTION(InvaildParameterException, "no element in current space!");
		}
	}

	void clear()
	{
		m_queue_1.clear();
		m_queue_2.clear();
	}

	int size() const
	{
		return m_queue_1.length() + m_queue_2.length();
	}

	~QueueToStack()
	{
		clear();
	}
};

}






#endif